package com.ruoyi.web.controller.sxt;

import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SxtBumen;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.sxt.service.ISxtBumenService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 部门Controller
 * 
 * @author ruoyi
 * @date 2025-06-25
 */
@Controller
@RequestMapping("/sxt/bumen")
public class SxtBumenController extends BaseController
{
    private String prefix = "sxt/bumen";

    @Autowired
    private ISxtBumenService sxtBumenService;

    @RequiresPermissions("sxt:bumen:view")
    @GetMapping()
    public String bumen()
    {
        return prefix + "/bumen";
    }

    /**
     * 查询部门列表
     */
    @RequiresPermissions("sxt:bumen:list")
    @PostMapping("/list")
    @ResponseBody
    public List<SxtBumen> list(SxtBumen sxtBumen)
    {
        List<SxtBumen> list = sxtBumenService.selectSxtBumenList(sxtBumen);
        return list;
    }

    /**
     * 导出部门列表
     */
    @RequiresPermissions("sxt:bumen:export")
    @Log(title = "部门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SxtBumen sxtBumen)
    {
        List<SxtBumen> list = sxtBumenService.selectSxtBumenList(sxtBumen);
        ExcelUtil<SxtBumen> util = new ExcelUtil<SxtBumen>(SxtBumen.class);
        return util.exportExcel(list, "部门数据");
    }

    /**
     * 新增部门
     */
    @RequiresPermissions("sxt:bumen:add")
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
    {
        SxtBumen sxtBumen = sxtBumenService.selectSxtBumenByDeptId(parentId);
        mmap.put("dept", sxtBumen);
        return prefix + "/add";
    }

    /**
     * 新增保存部门
     */
    @RequiresPermissions("sxt:bumen:add")
    @Log(title = "部门", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SxtBumen sxtBumen)
    {
        return toAjax(sxtBumenService.insertSxtBumen(sxtBumen));
    }

    /**
     * 修改部门
     */
    @RequiresPermissions("sxt:bumen:edit")
    @GetMapping("/edit/{deptId}")
    public String edit(@PathVariable("deptId") Long deptId, ModelMap mmap)
    {
        SxtBumen sxtBumen = sxtBumenService.selectSxtBumenByDeptId(deptId);
        sxtBumen.setParentName(sxtBumenService.selectSxtBumenByDeptId(sxtBumen.getParentId()).getDeptName());
        mmap.put("sxtBumen", sxtBumen);
        return prefix + "/edit";
    }

    /**
     * 修改保存部门
     */
    @RequiresPermissions("sxt:bumen:edit")
    @Log(title = "部门", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SxtBumen sxtBumen)
    {
        return toAjax(sxtBumenService.updateSxtBumen(sxtBumen));
    }

    /**
     * 删除部门
     */
    @RequiresPermissions("sxt:bumen:remove")
    @Log(title = "部门", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sxtBumenService.deleteSxtBumenByDeptIds(ids));
    }

    /**
     * 选择部门树
     *
     * @param deptId 部门ID
     */
    @RequiresPermissions("system:user:list")
    @GetMapping("/selectDeptTree/{deptId}")
    public String selectDeptTree(@PathVariable("deptId") Long deptId, ModelMap mmap)
    {
        mmap.put("dept",sxtBumenService.selectSxtBumenByDeptId(deptId));
        return prefix + "/tree";
    }

    /**
     * 加载部门列表树
     */
    @RequiresPermissions("system:user:list")
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> deptTreeData()
    {
        List<Ztree> ztrees = sxtBumenService.selectDeptTree(new SxtBumen());
        return ztrees;
    }

}
